/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var name="",desc="",type="",year="";
$(document).ready(function(){
   var btnRecord;
   initialize();
   function initialize(){
       btnRecord = $('#btnRecord');
       $(btnRecord).click(function(){
           if(type===""){
               alert("Please choose type of movie");
           }
           else if(year===""){
               alert("Please choose year of movie");
           }else if($('input:first').val()===""){
               alert("Please insert name of movie");
           }else if($('textarea').val()===""){
               alert("Please insert description of movie");
           }
           else{
                name = $('input:first').val();
                desc = $('textarea').val();
                type = $('#type').text();
                year = $('#year').text();
                addRow();
                save();
           }
       });
       $('ul:first>li').click(function(){
           type = $(this).text();
           $('#type').text(type);
       });
       $('ul:last>li').click(function(){
           year = $(this).text();
           $('#year').text(year);
       });
       $('#btnLoad').click(function(){
           load();
       });
   }
   
   function addRow(){
       var numRows = $('table tr').size();
       var row = document.createElement("tr");
       var col = new Array(4);
       for(var i=0;i<4;i++){
           col[i] = document.createElement("td"); 
       }
       $(col[0]).append(numRows);
       $(col[1]).append($('input').eq(0).val());
       $(col[2]).append(type);
       $(col[3]).append(year);
       $(row).append(col);
       $('#table1').append(row);
   }
   
   function save(){
       $.ajax({
           type : "POST",
           url : "php/save.php",
           data : {name : name,desc : desc,type : type,year : year},
           success : function(echo_msg){
                alert(echo_msg);
           }
       });
   }
   
   function load(){
       $.ajax({
          type : "POST",
          url : "php/load.php",
          success : function(echo_msg){
              clearTable();
              var rows = echo_msg.split("|");
              var col,row,td;
              var table = $('#table1');
              for(var i=0;i<rows.length-1;i++){
                  row = document.createElement("tr");
                  col = rows[i].split(":");
                  td = document.createElement("td");
                  $(td).append(i+1);
                  $(row).append(td);
                  td = document.createElement("td");
                  $(td).append(col[0]);
                  $(row).append(td);
                  td = document.createElement("td");
                  $(td).append(col[2]);
                  $(row).append(td);
                  td = document.createElement("td");
                  $(td).append(col[3]);
                  $(row).append(td);
                  $(table).append(row);
              }
          }
       });
   }
   
   function clearTable(){
       var row = document.createElement("tr");
       var th = document.createElement("th");
       $(th).append("No.");
       $(row).append(th);
       th = document.createElement("th");
       $(th).append("Name");
       $(row).append(th);
       th = document.createElement("th");
       $(th).append("Type");
       $(row).append(th);
       th = document.createElement("th");
       $(th).append("Year");
       $(row).append(th);
       $('tr').remove();
       $('table').append(row);
   }
});
